//********************************************************************
// 
//  file:  StoredFileSamples.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.Text;

namespace Diomede.SDK.Example.Samples.Files
{
    public class StoredFileSamples
    {
        static void moveFileToArchive()
        {
            ISession session = new Session();
            if (session.Login("username", "password").Status != Enums.ExecutionResults.Success)
            {
                Console.WriteLine(string.Format("Failed to login to Diomede service."));
                return;
            }

            int fileId = 15; //sample file id
            IFileManagement fileMgr = session as IFileManagement;
            //create session instance of the file
            IStoredFile file = fileMgr.StoredFileInstance(fileId);

            //move the StoreFile to Archive
            IStoredFileExecResult result = file.MoveToTier(Diomede.SDK.Enums.StorageInfo.ArchiveStorage);
            if (result.Status == Enums.ExecutionResults.Success)
                Console.WriteLine("File is successfully moved to archive.");
            else
                Console.WriteLine(string.Format("Failed to move file, error: {0}", result.Message));
        }

        static void getDownloadUrl()
        {
            ISession session = new Session();
            if (session.Login("username", "password").Status != Enums.ExecutionResults.Success)
            {
                Console.WriteLine(string.Format("Failed to login to Diomede service."));
                return;
            }

            int fileId = 15; //sample file id
            IFileManagement fileMgr = session as IFileManagement;
            //create session instance of the file
            IStoredFile file = fileMgr.StoredFileInstance(fileId);

            //get the download url
            string url = file.GetDownloadUrl(5, null, null, "http://mydomain.com/error.aspx");

            if (string.IsNullOrEmpty(url))
            {
                Console.WriteLine(string.Format("Download url is {0}", url));
            }
        }

        static void deleteAndUndeleteFile()
        {
            ISession session = new Session();
            if (session.Login("username", "password").Status != Enums.ExecutionResults.Success)
            {
                Console.WriteLine(string.Format("Failed to login to Diomede service."));
                return;
            }

            int fileId = 15; //sample file id
            IFileManagement fileMgr = session as IFileManagement;
            //create session instance of the file
            IStoredFile file = fileMgr.StoredFileInstance(fileId);

            //delete the file
            IStoredFileExecResult result;
            //delete the file
            result = file.Delete();
            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                Console.WriteLine("File successfully deleted.");
                //undelete the file
                result = file.Undelete();
                if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                    Console.WriteLine("File is successfully undeleted.");
            }
        }

    }
}
